//
//  LGEditTeamContentController.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2023/2/18.
//  编辑团队名称、介绍

#import "LGEditTeamContentController.h"
#import <IQKeyboardManager/IQKeyboardManager.h>
@interface LGEditTeamContentController ()<UITextViewDelegate>
/**   */
@property (nonatomic , strong)UITextView *textView;
/**   */
@property (nonatomic , strong)UIButton *saveBtn;
/**   */
@property (nonatomic , strong)UILabel *numberLabel;
/**   */
@property (nonatomic , copy)NSString *placeHolder;
/**   */
@property (nonatomic , assign)NSInteger count;
@end

@implementation LGEditTeamContentController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.】
    self.placeHolder = @"请输入...";
    [self.view addSubview:self.textView];
    [self.view addSubview:self.numberLabel];
    [IQKeyboardManager sharedManager].enableAutoToolbar = NO;
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:self.saveBtn];
    [self.numberLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.textView.mas_bottom).offset(viewPix(10));
        make.centerX.equalTo(self.view);
    }];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
}
-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    [self.textView becomeFirstResponder];
}

-(void)saveBtnAction{
    NSString *content = [self.textView.text stringByReplacingOccurrencesOfString:@" " withString:@""];
    if(content.length == 0 || [content isEqualToString:self.placeHolder]){
        [TooltipView showMessage:@"请先输入内容" offset:0];
        return;
    }
    [RequestUtil withPOST:@"/doctor/my.teams/saveTeamInfo" parameters:@{@"type":self.type,@"teamid":self.teamId,@"content":self.textView.text} withSign:YES success:^(NSInteger code, NSString *message, id returnData) {
        if(code == 200){
            [TooltipView showMessage:@"保存成功" offset:0];
            [self.navigationController popViewControllerAnimated:YES];
        }else{
            [TooltipView showMessage:message offset:0];
        }
    } error:^(NSError *error) {
        [TooltipView showMessage:LGErrorMessage offset:0];
    }];
}

-(void)setType:(NSString *)type{
    _type = type;
    self.count = [type isEqualToString:@"title"] ? 20:500;
    self.title = [type isEqualToString:@"title"] ? @"编辑团队名称":@"编辑团队介绍";
    self.numberLabel.text = [NSString stringWithFormat:@"0/%ld",self.count];
}


-(void)setContent:(NSString *)content{
    _content = content;
    self.textView.text = content;
    if(content.length == 0 || [content isEqualToString:self.placeHolder]){
        self.textView.text = self.placeHolder;
        self.textView.textColor = [UIColor colorWithString:@"#9595A0"];
    }else{
        self.textView.textColor = [UIColor colorWithString:@"#333333"];
    }
}

- (void)textViewDidBeginEditing:(UITextView *)textView{
    self.textView.textColor = [UIColor colorWithString:@"#333333"];
    if([textView.text isEqualToString:self.placeHolder]){
        self.textView.text = @"";
    }
}
- (void)textViewDidEndEditing:(UITextView *)textView{
    NSString *content = textView.text;
    if(content.length == 0||[textView.text isEqualToString:self.placeHolder]){
        self.textView.text = self.placeHolder;
        self.textView.textColor = [UIColor colorWithString:@"#9595A0"];
    }else{
        self.textView.textColor = [UIColor colorWithString:@"#333333"];
    }
}

- (void)textViewDidChange:(UITextView *)textView{
    NSString *lang = textView.textInputMode.primaryLanguage; // 键盘输入模式
    NSString *content = textView.text;
    if ([lang isEqualToString:@"zh-Hans"]){
        UITextRange *selectedRange = [textView markedTextRange];
        if (!selectedRange) { // 没有高亮
            textView.text = (textView.text.length>self.count)?[textView.text substringToIndex:self.count]:textView.text;
            self.numberLabel.text = [NSString stringWithFormat:@"%ld / %ld",textView.text.length,self.count];
        }
    }else{
        textView.text = (textView.text.length>self.count)?[textView.text substringToIndex:self.count]:textView.text;
        self.numberLabel.text = [NSString stringWithFormat:@"%ld / %ld",textView.text.length,self.count];
    }
}


-(void)keyboardWillShow:(NSNotification *)notification{
    NSInteger duration = [notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] integerValue];
    CGRect keyboardFrame = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
    CGFloat height = keyboardFrame.size.height;
    self.textView.frame = CGRectMake(viewPix(16), viewPix(10), Screen_W-viewPix(32), Screen_H-topBarHeight-viewPix(45)-height);
    self.numberLabel.hidden = NO;
}
-(UITextView *)textView{
    if(!_textView){
        _textView = [[UITextView alloc]initWithFrame:CGRectMake(viewPix(16), viewPix(10), Screen_W-viewPix(32), viewPix(260))];
        _textView.backgroundColor = [UIColor colorWithString:@"#F9F9F9"];
        _textView.tintColor = [UIColor colorWithString:@"#333333"];
        _textView.textColor = [UIColor colorWithString:@"#333333"];
        _textView.textAlignment = NSTextAlignmentLeft;
        _textView.font = LGFont(16);
        _textView.delegate = self;
    }
    return _textView;
}

-(UIButton *)saveBtn{
    if(!_saveBtn){
        _saveBtn = [UIButton buttonWithTitle:@"保存" titleFont:LGFontWeight(14, UIFontWeightMedium) textColor:@"#429BFF" imageName:@""];
        _saveBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
        _saveBtn.frame = CGRectMake(0, 0, 60, 44);
        [_saveBtn addTarget:self action:@selector(saveBtnAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _saveBtn;
}

-(UILabel *)numberLabel{
    if(!_numberLabel){
        _numberLabel = [UILabel lableWithText:@"0 / 500" colorString:@"#999999" textFont:LGFont(14) textAlignment:NSTextAlignmentCenter lines:1];
        _numberLabel.hidden = YES;
    }
    return _numberLabel;
}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
